package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.pojo.Detail;
import com.bwie.pojo.Dishes;
import com.bwie.pojo.Linkage;
import com.bwie.service.DetailService;
import com.bwie.service.LinkageService;
import com.bwie.service.ListService;
import com.bwie.unit.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("user")
public class DishesController {

    @Autowired
    private ListService listService;

    @Autowired
    private DetailService detailService;

    @Resource
    private LinkageService linkageService;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("PageList")
    public R PageList(@RequestBody Dishes dishes){
        Page<Dishes> dishesPage = new Page<>(dishes.getPageNum(), dishes.getPageSize());
        Page<Dishes> pageList = listService.selectDishes(dishesPage);
        return R.OK(pageList);
    }

    @PostMapping("addDetail")
    public R addDetail(@RequestBody Dishes dishes){
        Detail detail = new Detail();
        detail.setName(dishes.getDishesName());
        detail.setPrice(dishes.getDishesPrice());
        detail.setNums(dishes.getDishesNums());
        detailService.save(detail);
        return R.OK(detail);
    }



     @PostMapping("getLinkageList/{id}")
     public R getLinkageList(@PathVariable Integer id){
         LambdaQueryWrapper<Linkage> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(Linkage::getDishesId,id);
         List<Linkage> list = linkageService.list(wrapper);
         return R.OK(list);
     }

    @PostMapping("addDishes")
    public R addDishes(@RequestBody Dishes dishes){
            dishes.setDishesCreationtime(new Date());
            listService.save(dishes);
            return R.OK();
    }


    @PostMapping("getById/{id}")
    public R getById(@PathVariable Integer id){
        Boolean b = stringRedisTemplate.opsForSet().isMember("text", id.toString());
        if (b){
            stringRedisTemplate.opsForSet().remove("text",id.toString());
            return R.OK("取消收藏成功");
        }else {
            stringRedisTemplate.opsForSet().add("text",id.toString());
            return R.OK("收藏成功");
        }
    }


}
